package teamone;

import robocode.HitWallEvent;
import robocode.HitRobotEvent;
import robocode.ScannedRobotEvent;
import robocode.MessageEvent;
import robocode.TeamRobot;
import robocode.RobotDeathEvent;
import java.util.Random;
import java.awt.Color;
import java.io.IOException;

public class SimpleDrone extends TeamRobot {
	
	protected static double oldEnemyHeading;
	static boolean battleMode = false;
	static boolean scanningMode = true;
	static String enemyName;
	static String[] enemyNamesArray;
	static double[][] enemyPositionMatrix;
	static double[] enemyHealthArray;
	static double[][] enemyDistanceMatrix;
	static double[] teammatesHealthArray;
	static String[] teammatesNamesArray;
	static int allTeammates;
	static int fallenTeammates;
	static int allOtherRobots;
	static double previousEnergy;
	static double radiusCircleEnemy;
	static double circleSectorLength;
	static int movementDirection;
	static int gunDirection;
	static double fieldWidth;
	static double fieldHeight;
	static double robotWidth;
	static double robotHeight;
	
	//the initial function
	public void run(){
		battleMode = false;
		scanningMode = true;
		allTeammates = 0;
		fallenTeammates = 0;
		allOtherRobots = 0;
		oldEnemyHeading = 0;
		previousEnergy = 100;
		radiusCircleEnemy = 0;
		circleSectorLength = 200;
		movementDirection = 1;
		gunDirection = 1;
		Random randomGenerator = new Random();
		allTeammates = getTeammates().length;
		allOtherRobots = getOthers();
		enemyNamesArray = new String[allOtherRobots - allTeammates];
		enemyPositionMatrix = new double[allOtherRobots - allTeammates][2];
		enemyHealthArray = new double[allOtherRobots - allTeammates];
		enemyDistanceMatrix = new double[allOtherRobots - allTeammates][allTeammates];
		teammatesHealthArray = new double[allTeammates];
		teammatesNamesArray = getTeammates();
		int XPoint;
		int YPoint;
		fieldWidth = getBattleFieldWidth();
		fieldHeight = getBattleFieldHeight();
		robotWidth = getWidth();
		robotHeight = getHeight();
		setBodyColor(Color.BLUE);
		setRadarColor(Color.WHITE);
		setGunColor(Color.RED);
		setBulletColor(Color.RED);
		setScanColor(Color.WHITE);
		for(int i = 0; i < enemyNamesArray.length; i++){
			enemyNamesArray[i] = "void";
		}
		while(true){
			if(scanningMode){
				XPoint = randomGenerator.nextInt((int)(fieldWidth));
				YPoint = randomGenerator.nextInt((int)(fieldHeight));
				Scanning.scanArea(this, (double)(XPoint), (double)(YPoint));
				/*for(int i = 0; i < enemyNamesArray.length; i++){
					System.out.println(enemyNamesArray[i]);
				}
				for(int i = 0; i < enemyHealthArray.length; i++){
					System.out.println(enemyHealthArray[i]);
				}*/
				System.out.println("scannedArea");
				Scanning.decideOnNextEnemy(this);			
			}
			if(battleMode){
					for(int i = 0; i < enemyNamesArray.length; i++){
						if(enemyNamesArray[i].equals(enemyName)){
							//Scanning.scanArea(this, getX(), getY());
							Movement.move(this, enemyPositionMatrix[i][0] + (double)(randomGenerator.nextInt(80)) - 40.0, enemyPositionMatrix[i][1] + (double)(randomGenerator.nextInt(80)) - 40.0);
							System.out.println("movedToEnemyPosition");
						}
					}
			}			
		}
	}
	
	//function gets called when robot is hit by bullet
	public void onHitByBullet(){		
	}
	
	//function gets called when robot hits other robot
	public void onHitRobot(HitRobotEvent event){		
	}
	
	//function gets called when robot hits wall
	public void onHitWall(HitWallEvent event){
		//turnRight(event.getBearing());
	}
	
	//function gets called when robot scans other robot
	public void onScannedRobot(ScannedRobotEvent event){
		String eventName = event.getName();
		double eventDistance = event.getDistance();
		if(Movement.AusweichRobot){
			Movement.Ausweichen(this, event, Movement.statZielX, Movement.statZielY);
			return;
		}
		if(!isTeammate(eventName)){
			boolean enemyFound = false; 
			double enemyXCoordinate = (double) (this.getX() + eventDistance * Math.sin(this.getHeadingRadians() + event.getBearingRadians()));
			double enemyYCoordinate = (double) (this.getY() + eventDistance * Math.cos(this.getHeadingRadians() + event.getBearingRadians()));
			//save the scanned data in different arrays
			for(int i = 0; i < enemyNamesArray.length; i++){
				if(eventName.equals(enemyNamesArray[i])){
					enemyFound = true;
					enemyHealthArray[i] = event.getEnergy();
					enemyPositionMatrix[i][0] = enemyXCoordinate;
					enemyPositionMatrix[i][1] = enemyYCoordinate;
					/*for (int j = 0; j < getTeammates().length; j++){
						if(teammatesNamesArray[j].equals(sender)){
							enemyDistanceMatrix[i][j] = Double.parseDouble(enemyDistanceString);
						}
					}*/
				}
			}
			if(!enemyFound){
				for(int i = 0; i < enemyNamesArray.length; i++){
					if(enemyNamesArray[i].equals("void")){
						enemyNamesArray[i] = eventName;
						enemyHealthArray[i] = event.getEnergy();
						enemyPositionMatrix[i][0] = enemyXCoordinate;
						enemyPositionMatrix[i][1] = enemyYCoordinate;
						break;
						/*for (int j = 0; j < getTeammates().length; j++){
							if(teammatesNamesArray[j].equals(sender)){
								enemyDistanceMatrix[i][j] = Double.parseDouble(enemyDistanceString);
							}
						}*/
					}
				}
			}
			try{
				broadcastMessage(eventName + "/" + event.getEnergy() + "/" + eventDistance + "/" + enemyXCoordinate + "/" + enemyYCoordinate + "/");
			}
			catch(IOException exception){
				System.out.println("Shit hit the fan.");
			}
			if(battleMode){
				if((eventName).equals(enemyName)){
					setTurnRight(event.getBearing());
					setTurnRadarRight(getHeading() - getRadarHeading() + event.getBearing());
						if(eventDistance < (fieldHeight/1.4)){
							//fire(3);
							Battle.circularShooting(this, event, 3.0);
							//Battle.circleEnemyAndShoot(this, event);
						}
						else if(eventDistance < (fieldHeight/2)){
							//fire(2);
							setAhead(eventDistance/4);
							Battle.circularShooting(this, event, 2.0);
							//Battle.circleEnemyAndShoot(this, event);
						}
						else if(eventDistance < (fieldHeight/1.5)){
							//fire(1);
							setAhead(eventDistance/2);
							Battle.circularShooting(this, event, 1.0);
							//Battle.circleEnemyAndShoot(this, event);
						}
				}
				
			}
			/*if(event.getDistance() < (getBattleFieldHeight()/3)){
					//fire(3);
					Battle.circularShooting(this, event, 3.0);
			}
			else if(event.getDistance() < (getBattleFieldHeight()/2)){
					//fire(2);
					Battle.circularShooting(this, event, 2.0);
			}
			else if(event.getDistance() < (getBattleFieldHeight()/1.5)){
					//fire(1);
					Battle.circularShooting(this, event, 1.0);
			}*/
		}
	}
	
	public void onRobotDeath(RobotDeathEvent event){
		boolean isEnemy = false;
		for(int i = 0; i < enemyNamesArray.length; i++){
			if(event.getName().equals(enemyNamesArray[i])){
				isEnemy = true;
			}
		}
		if(!isEnemy){
			fallenTeammates += 1; 
		}
		else{
			for(int i = 0; i < enemyNamesArray.length; i++){
				if(event.getName().equals(enemyNamesArray[i])){
					enemyHealthArray[i] = 0.0;
					enemyPositionMatrix[i][0] = 0.0;
					enemyPositionMatrix[i][1] = 0.0;
					/*for (int j = 0; j < getTeammates().length; j++){
						if(teammatesNamesArray[j].equals(sender)){
							enemyDistanceMatrix[i][j] = Double.parseDouble(enemyDistanceString);
						}
					}*/
				}
			}
			battleMode = false;
			scanningMode = true;
			if((getOthers()- (allTeammates - fallenTeammates)) > 0){
				Scanning.decideOnNextEnemy(this);
			}
			//Scanning.scanArea(this, getX(), getY());
		}
	}
	
	//function gets called when robot receives message
	public void onMessageReceived(MessageEvent event){	
		//if(!battleMode){
			int tokenCounter = 0;
			String message = (String)(event.getMessage());
			//String sender = event.getSender();
			String enemyString = new String();
			String enemyHealthString = new String();
			String enemyDistanceString = new String();
			String enemyXCoordinate = new String();
			String enemyYCoordinate = new String();
			boolean enemyFound = false;
			//parse the received message
			while(message.charAt(tokenCounter) != '/'){
				enemyString = enemyString + message.charAt(tokenCounter);
				tokenCounter += 1;
			}
			tokenCounter += 1;
			while(message.charAt(tokenCounter) != '/'){
				enemyHealthString = enemyHealthString + message.charAt(tokenCounter);
				tokenCounter += 1;
			}
			tokenCounter += 1;
			while(message.charAt(tokenCounter) != '/'){
				enemyDistanceString = enemyDistanceString + message.charAt(tokenCounter);
				tokenCounter += 1;
			}
			tokenCounter += 1;
			while(message.charAt(tokenCounter) != '/'){
				enemyXCoordinate = enemyXCoordinate + message.charAt(tokenCounter);
				tokenCounter += 1;
			}
			tokenCounter += 1;
			while(message.charAt(tokenCounter) != '/'){
				enemyYCoordinate = enemyYCoordinate + message.charAt(tokenCounter);
				tokenCounter += 1;
			}
			//??????
			//save the received data in different arrays
			for(int i = 0; i < enemyNamesArray.length; i++){
				if(enemyString.equals(enemyNamesArray[i])){
					enemyFound = true;
					enemyHealthArray[i] = Double.parseDouble(enemyHealthString);
					enemyPositionMatrix[i][0] = Double.parseDouble(enemyXCoordinate);
					enemyPositionMatrix[i][1] = Double.parseDouble(enemyYCoordinate);
					/*for (int j = 0; j < getTeammates().length; j++){
						if(teammatesNamesArray[j].equals(sender)){
							enemyDistanceMatrix[i][j] = Double.parseDouble(enemyDistanceString);
						}
					}*/
				}
			}
			if(!enemyFound){
				for(int i = 0; i < enemyNamesArray.length; i++){
					if(enemyNamesArray[i].equals("void")){
						enemyNamesArray[i] = enemyString;
						enemyHealthArray[i] = Double.parseDouble(enemyHealthString);
						enemyPositionMatrix[i][0] = Double.parseDouble(enemyXCoordinate);
						enemyPositionMatrix[i][1] = Double.parseDouble(enemyYCoordinate);
						break;
						/*for (int j = 0; j < getTeammates().length; j++){
							if(teammatesNamesArray[j].equals(sender)){
								enemyDistanceMatrix[i][j] = Double.parseDouble(enemyDistanceString);
							}
						}*/
					}
				}
			}
		//}
	}
}
